package com.pl.study.mapreduce;

import com.pl.study.mapreduce.vo.FlowVo;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * <p>
 *
 * @Description: map
 * </p>
 * @ClassName FlowStatisticMap
 * @Author pl
 * @Date 2022/3/10
 * @Version V1.0.0
 */
public class FlowStatisticMap extends Mapper<LongWritable, Text, Text, FlowVo> {

    private Text outK = new Text();
    private FlowVo outV = new FlowVo();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String line = value.toString();
        String[] split = line.split("\t");

        String phone = split[1];
        String up = split[split.length - 3];
        String down  = split[split.length - 2];

        //封装
        outK.set(phone);
        long upLong = Long.parseLong(up);
        outV.setUpFlow(upLong);
        long downLong = Long.parseLong(down);
        outV.setDownFlow(downLong);
        outV.setSumFlow(upLong+downLong);

        //写出
        context.write(outK, outV);
    }
}